package com.r2coding.auth.mobile;

import com.r2coding.auth.exception.BizCode;
import com.r2coding.auth.exception.ServiceException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author:98k灬
 * @since:2022/05/23 22:01
 **/
@Component
public class MobileValidateFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain chain) throws ServletException, IOException {
        System.out.println(request.getRequestURI());
        System.out.println(request.getMethod());
        if ("/mobile/login".equals(request.getRequestURI()) && "post".equalsIgnoreCase(request.getMethod())) {
            try {
                validate(request);
            } catch (Exception e) {

                return;
            }
        }
    }

    private void validate(HttpServletRequest request) {
        // 获取请求中携带的验证码
        String code = request.getParameter("code");
        if (StringUtils.isEmpty(code)) {
            throw new ServiceException(BizCode.PHONE_CODE_EMPTY);
        }
        // TODO 从redis中读取验证码并判断正确性
    }
}
